
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Reading oscilloscope data &mdash; Ripyl 1.2 documentation</title>
    
    <link rel="stylesheet" href="../_static/nature.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/ripyl.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="top" title="Ripyl 1.2 documentation" href="../index.html" />
    <link rel="next" title="Common data structures" href="data_structures.html" />
    <link rel="prev" title="Ripyl tutorials" href="tutorial.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="data_structures.html" title="Common data structures"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="tutorial.html" title="Ripyl tutorials"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="reading-oscilloscope-data">
<h1>Reading oscilloscope data<a class="headerlink" href="#reading-oscilloscope-data" title="Permalink to this headline">¶</a></h1>
<p>Ripyl does not provide any support for reading data formats directly produced by oscilloscopes and logic analyzers. A user of the library will have to take care of that independently before decoding with Ripyl. Most instruments will produce some form of CSV output that can be easily parsed with the <a class="reference external" href="http://docs.python.org/2/library/csv.html">csv module</a> in the Python standard library. There is no standard organization for CSV data so reading functions will be manufacturer and (possibly) model specific. Your instruments may also support other text based formats such as Matlab arrays that can be easily read by a Python script. If your device supports binary output, however, you will be able to read large data sets more rapidly.</p>
<p>Here are some example functions for reading data from various oscilloscopes.</p>
<div class="section" id="lecroy">
<h2>LeCroy<a class="headerlink" href="#lecroy" title="Permalink to this headline">¶</a></h2>
<p>Most LeCroy oscilloscopes support the same binary and CSV formats. Here is an example function for reading the CSV files produced by these instruments.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">csv</span>

<span class="k">def</span> <span class="nf">read_lecroy_csv</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span>
    <span class="n">sample_period</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="n">raw_samples</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">csvfile</span><span class="p">:</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="n">csvfile</span><span class="p">)</span>

        <span class="c"># Sample period is in cell B2 (1,1)</span>
        <span class="c"># Time is in column D (3)</span>
        <span class="c"># Samples are in column E (4)</span>

        <span class="k">for</span> <span class="n">row_num</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">row_num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="c"># get the sample period</span>
                <span class="n">sample_period</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">break</span>

        <span class="n">csvfile</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
            <span class="n">raw_samples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">4</span><span class="p">]))</span>

    <span class="k">return</span> <span class="n">raw_samples</span><span class="p">,</span> <span class="n">sample_period</span>
</pre></div>
</div>
</div>
<div class="section" id="rigol">
<h2>Rigol<a class="headerlink" href="#rigol" title="Permalink to this headline">¶</a></h2>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">csv</span>

<span class="k">def</span> <span class="nf">read_rigol_csv</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="n">channel</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span>
    <span class="n">sample_period</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="n">raw_samples</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">sample_count</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">csvfile</span><span class="p">:</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="n">csvfile</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">row_num</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">row_num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">sample_period</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span>
                <span class="n">sample_count</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">3</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&#39;:&#39;</span><span class="p">)[</span><span class="mi">1</span><span class="p">])</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">row</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;X&#39;</span><span class="p">:</span>
                <span class="k">break</span>

        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">sample_count</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">raw_samples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[(</span><span class="n">channel</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span><span class="o">*</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]))</span>

            <span class="n">sample_count</span> <span class="o">-=</span> <span class="mi">1</span>

    <span class="k">return</span> <span class="n">raw_samples</span><span class="p">,</span> <span class="n">sample_period</span>
</pre></div>
</div>
</div>
<div class="section" id="tektronix">
<h2>Tektronix<a class="headerlink" href="#tektronix" title="Permalink to this headline">¶</a></h2>
<p>Tektronix hasn&#8217;t maintained a consistent CSV format across its product lines. Here is an example for the TDS2000 series.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">csv</span>

<span class="k">def</span> <span class="nf">read_tek_tds2000_csv</span><span class="p">(</span><span class="n">fname</span><span class="p">):</span>
    <span class="n">sample_period</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="n">raw_samples</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">fname</span><span class="p">,</span> <span class="s">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">csvfile</span><span class="p">:</span>
        <span class="n">c</span> <span class="o">=</span> <span class="n">csv</span><span class="o">.</span><span class="n">reader</span><span class="p">(</span><span class="n">csvfile</span><span class="p">)</span>

        <span class="c"># Sample period is in cell B2 (1,1)</span>

        <span class="k">for</span> <span class="n">row_num</span><span class="p">,</span> <span class="n">row</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">c</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">row_num</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="c"># get the sample period</span>
                <span class="n">sample_period</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
                <span class="k">break</span>

        <span class="c"># Sample data starts after the last header line</span>
        <span class="c"># containing the firmware version.</span>
        <span class="n">in_header</span> <span class="o">=</span> <span class="bp">True</span>
        <span class="k">for</span> <span class="n">row</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">in_header</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">row</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&#39;Firmware Version&#39;</span><span class="p">:</span>
                    <span class="n">in_header</span> <span class="o">=</span> <span class="bp">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">raw_samples</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">float</span><span class="p">(</span><span class="n">row</span><span class="p">[</span><span class="mi">4</span><span class="p">]))</span>

    <span class="k">return</span> <span class="n">raw_samples</span><span class="p">,</span> <span class="n">sample_period</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/Ripyl logo 100px.png" alt="Logo"/>
            </a></p>
  <h3><a href="../index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Reading oscilloscope data</a><ul>
<li><a class="reference internal" href="#lecroy">LeCroy</a></li>
<li><a class="reference internal" href="#rigol">Rigol</a></li>
<li><a class="reference internal" href="#tektronix">Tektronix</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="tutorial.html"
                        title="previous chapter">Ripyl tutorials</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="data_structures.html"
                        title="next chapter">Common data structures</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="../_sources/rst/reading_data.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="data_structures.html" title="Common data structures"
             >next</a> |</li>
        <li class="right" >
          <a href="tutorial.html" title="Ripyl tutorials"
             >previous</a> |</li>
        <li><a href="../index.html">Ripyl 1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2013, Kevin Thibedeau.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>

<script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
            (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-43149311-1', 'google.com');
  ga('send', 'pageview');

</script>

  </body>
</html>